//# sourceURL=http://uMapApp/Earth/CameraRound
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "./ViewPort"
], function (
    mhjs,
    uMap,
    uEarth,
    ViewPort
) {

    function CameraRound(option) {

        var _this = this;

        _this.earth = option.earth;
        _this.center = option.center;
        _this.speed = option.speed || 1;
        _this.pitch = mhjs.defined(option.pitch) ? option.pitch : -45;
        _this.range = mhjs.defined(option.range) ? option.range : 1000;
        _this.startHeading = mhjs.defined(option.startHeading) ? option.startHeading : 0;
        _this.repeat = mhjs.defined(option.repeat) ? option.repeat : true;

        // _this.clock = new uEarth.Clock();
        _this.clock = _this.earth.clock;
        _this.circleTime = 60;

    }

    var _class = CameraRound;

    Object.defineProperties(_class.prototype, {
        center        : {
            get: function () {
                return this._center;
            },
            set: function (value) {
                this._center = new uMap.Position(value);
            }
        },
        speed         : {
            get: function () {
                return this._speed;
            },
            set: function (value) {
                this._speed = value;
            }
        },
        pitch         : {
            get: function () {
                return this._pitch;
            },
            set: function (value) {
                this._pitch = value;
            }
        },
        range         : {
            get: function () {
                return this._range;
            },
            set: function (value) {
                this._range = value;
            }
        },
        repeat        : {
            get: function () {
                return this._repeat;
            },
            set: function (value) {
                this._repeat = value;
            }
        },
        setTimeoutView: {
            value: function () {
                var _this = this;

                var diffSeconds = uEarth.JulianDate.secondsDifference(_this.clock.currentTime, _this.clock.startTime);
                var heading = _this.startHeading + diffSeconds / _this.circleTime * 360;

                _this.earth.camera.lookAt(
                    _this.center.cartesian,
                    {
                        heading: uEarth.Math.toRadians(heading),
                        pitch  : uEarth.Math.toRadians(_this.pitch),
                        range  : _this.range
                    }
                );
            }
        },
        start         : {
            value: function () {
                var _this = this;
                _this.isStart = true;

                _this.trigger("start");
                _this.trigger("toggle");

                _this.earth.camera.flyToBoundingSphere(
                    new uEarth.BoundingSphere(
                        _this.center.cartesian,
                        _this.range
                    ),
                    {
                        offset  : {
                            heading: uEarth.Math.toRadians(_this.startHeading),
                            pitch  : uEarth.Math.toRadians(_this.pitch),
                            range  : _this.range
                        },
                        complete: function () {

                            _this.clock.startTime = _this.earth.clock.currentTime;
                            _this.clock.stopTime = uEarth.JulianDate.addSeconds(_this.clock.startTime, _this.circleTime, new uEarth.JulianDate());
                            _this.clock.canAnimate = true;
                            _this.clock.shouldAnimate = true;
                            _this.clock.clockRange = _this.repeat ? uEarth.ClockRange.LOOP_STOP : uEarth.ClockRange.CLAMPED;
                            _this.clock.multiplier = _this.speed;

                            _this.clock.onTick.addEventListener(_this.setTimeoutView, _this);
                            _this.clock.onStop.addEventListener(_this.stop, _this);
                        }
                    }
                );
            }
        },
        stop          : {
            value: function () {
                var _this = this;
                _this.clock.onTick.removeEventListener(_this.setTimeoutView, _this);
                _this.clock.onStop.removeEventListener(_this.stop, _this);
                _this.isStart = false;
                _this.earth.camera.lookAtTransform(uEarth.Matrix4.IDENTITY);
                _this.trigger("stop");
                _this.trigger("toggle");
            }
        },
        toggle        : {
            value: function (value) {
                if (this.isStart) {
                    this.stop();
                } else {
                    this.start();
                }
            }
        }
    });

    mhjs.EventInterface(_class);

    return _class;

});